package com.onpositive.knowledge.model.value.audits;

import java.util.Map;

import com.onpositive.knowledge.model.values.ValueTraverser;
import com.onpositive.semantic.model.api.property.IProperty;
import com.onpositive.units.ParsedIdent;
import com.onpositive.units.ParsedSentence;
import com.onpositive.units.ParsedString;
import com.onpositive.units.ParsedValue;
import com.onpositive.units.ParsedValueSequenceV2;
import com.onpositive.units.ValueCluster;

public class ValueCounterAudit extends ValueTraverser {
	
	@Override
	protected void process(IProperty prop, Object val, Map<String,Object> metaInfo, int index)
	{
		Object resultObject = metaInfo.get("audit-result");
		if( resultObject == null || !(resultObject instanceof ValueCounterAuditResult) )
			return ;
		
		ValueCounterAuditResult sar = (ValueCounterAuditResult) resultObject ;
		
		if( val instanceof ParsedString )
			sar.take(val) ;

		else if( val instanceof ParsedIdent )
			sar.take(val) ;

		else if ( val instanceof ParsedSentence ){
			ParsedSentence sentence = (ParsedSentence) val ;
			takeSequence( sentence, sar );
		}
		else if ( val instanceof ValueCluster ){				
			takeSequence( (ParsedValueSequenceV2) val, sar );
		}
		else
			return ;
		
	}
	
	private void takeSequence(ParsedValueSequenceV2 seq, ValueCounterAuditResult sar) {
		
		if( seq instanceof ParsedSentence )
			sar.take( seq ) ;
		
		else if ( seq instanceof ValueCluster ){
			ValueCluster<?> cluster = (ValueCluster<?>) seq ;
			if( cluster.getTargetClass() == ParsedIdent.class )
				sar.take(seq) ;
			else
				return ;
		}
	 
		for( ParsedValue pv : seq.getValuesArray() ){
			if( pv instanceof ParsedIdent )
				sar.take(pv) ;
			else if( pv instanceof ParsedValueSequenceV2 ){
				ParsedValueSequenceV2 newSeq = (ParsedValueSequenceV2) pv ;
				takeSequence(newSeq, sar) ;
			}
		}
	}

	@Override
	protected Class<?>[] createTargetClasses() {
		return new Class<?>[]{ ParsedIdent.class, ParsedSentence.class, ValueCluster.class, ParsedString.class };
	}
	
	@Override
	protected Class<?>[] createNecessaryClasses() {
		return new Class<?>[]{ ParsedIdent.class, ParsedSentence.class, ValueCluster.class, ParsedString.class };
	}
};
